package xin.qixia.client.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import xin.qixia.common.core.AutoQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import xin.qixia.client.mapper.RobotMapper;
import xin.qixia.client.service.IRobotService;
import xin.qixia.robot.core.domain.Robot;
import xin.qixia.robot.core.factory.RobotClientFactory;

import java.util.List;
import java.util.Collection;

/**
 * 机器人Service业务层处理
 *
 * @author wangkaixin
 * @date 2025-05-26
 */
@RequiredArgsConstructor
@Service
public class RobotServiceImpl implements IRobotService {

    private final RobotMapper baseMapper;

    private final RobotClientFactory factory;

    /**
     * 查询机器人
     *
     * @param robotId 主键
     * @return 机器人
     */
    @Override
    public Robot queryById(String robotId) {
        return baseMapper.selectById(robotId);
    }

    /**
     * 查询符合条件的机器人列表
     *
     * @param bo 查询条件
     * @return 机器人列表
     */
    @Override
    public List<Robot> queryList(Robot bo) {
        return baseMapper.selectList(buildQueryWrapper(bo));
    }

    private AutoQueryWrapper<Robot> buildQueryWrapper(Robot bo) {
        AutoQueryWrapper<Robot> wrapper = new AutoQueryWrapper<>();
        wrapper.eq(Robot::getType, bo.getType());
        wrapper.eq(Robot::getAccount, bo.getAccount());
        wrapper.like(Robot::getName, bo.getName());
        wrapper.eq(Robot::getClientId, bo.getClientId());
        wrapper.eq(Robot::getClientSecret, bo.getClientSecret());
        return wrapper;
    }

    /**
     * 修改机器人
     *
     * @param bo 机器人
     * @return 是否修改成功
     */
    @Override
    public Boolean save(Robot bo) {
        Robot robot = baseMapper.selectById(bo.getAccount());
        if (null != robot && robot.getStatus()) {
            throw new RuntimeException("机器人已启用，无法修改");
        }
        bo.setStatus(false);
        return baseMapper.insertOrUpdate(bo);
    }

    @Override
    public Boolean enable(String account) {
        Robot robot = baseMapper.selectById(account);
        if (null == robot) {
            throw new RuntimeException("机器人不存在");
        }
        if (robot.getStatus()) {
            throw new RuntimeException("机器人已启用，无需重复启用");
        }
        baseMapper.update(new LambdaUpdateWrapper<Robot>()
                .set(Robot::getStatus, true)
                .eq(Robot::getAccount, account));
        return factory.register(robot) != null;
    }

    @Override
    public Boolean disable(String account) {
        Robot robot = baseMapper.selectById(account);
        if (null == robot) {
            throw new RuntimeException("机器人不存在");
        }
        if (!robot.getStatus()) {
            throw new RuntimeException("机器人未启用，无需禁用");
        }
        baseMapper.update(new LambdaUpdateWrapper<Robot>()
                .set(Robot::getStatus, false)
                .eq(Robot::getAccount, account));
        return factory.cancel(account);
    }

    /**
     * 校验并批量删除机器人信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (baseMapper.selectCount(new AutoQueryWrapper<Robot>()
                .eq(Robot::getStatus, true)) > 0) {
            throw new RuntimeException("存在启用的机器人，无法删除");
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
